/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-10-26 15:52:42
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-10-29 09:16:23
 * @FilePath: /src/config.hpp
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#ifndef __MY_CONFIG__
#define __MY_CONFIG__
#include "util.hpp"

#include <mutex>
#include <iostream>
#define CONFIG_FILE "./cloud.conf"
#include "Log.hpp"
using namespace log_ns;
namespace cloud
{
    class Config
    {
    public:
        static Config *GetInstance()
        {
            if (_instance == nullptr)
            {
                _mutex.lock();
                if (_instance == nullptr)
                {
                    _instance = new Config();
                }
                _mutex.unlock();
            }
            return _instance;
        }
        
        int GetHosTime(){
            return _hot_time;
        }
        int GetServerPort(){
            return _server_port;
        }
        std::string GetServerIP()
        {
            return _server_ip;
        }
        std::string GetUrlPrefix(){
            return _url_prefix;
        }
        std::string GetArcSuffix()
        {
            return _arc_suffix;
        }
        std::string GetPackDir()
        {
            return _pack_dir;
        }
        std::string GetBackDir()
        {
            return _back_dir;
        }
        std::string GetManagerFile()
        {

            return _manager_file;
        }

        bool ReadConfigFile()
        {
            FileUtil fu(CONFIG_FILE);
            std::string body;
            if (fu.GetContent(&body) == false)
            {
                LOG(ERROR, "Load config file failed!\n");
                return false;
            }
            Json::Value root;
            if (JsonUtil::UnSerialize(body, &root) == false)
            {
                LOG(ERROR, "parse config file failed!\n");
                return false;
            }
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _url_prefix = root["url_prefix"].asString();
            _arc_suffix = root["arc_suffix"].asString();
            _pack_dir = root["pack_dir"].asString();
            _back_dir = root["back_dir"].asString();
            _manager_file = root["mananger_file"].asString();
            return true;
        }

    private:
        Config()
        {
            ReadConfigFile();
        }
        static Config *_instance;
        static std::mutex _mutex;

    private:
        int _hot_time;
        int _server_port;
        std::string _server_ip;
        std::string _url_prefix;
        std::string _arc_suffix;
        std::string _pack_dir;
        std::string _back_dir;
        std::string _manager_file;
    };
    Config *Config::_instance = nullptr;
    std::mutex Config::_mutex;
}

#endif